package cc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @ClassName: HelloServlet
 * @Description: hello
 * @Author: 晓东√
 * @Date: 2024-09-02 10:41
 * @Version: 1.0
 **/
@WebServlet(urlPatterns = "/hello.action")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");
       resp.setContentType("text/html;charset=UTF-8");
       resp.setCharacterEncoding("UTF-8");
        //获取前端传递的参数
        String name = req.getParameter("name");
        System.out.println("接收前端参数-----"+name);
        System.out.println("进入资源");


        req.setAttribute("hello","req:你进入了HelloServlet资源");

        HttpSession session = req.getSession();

        session.setAttribute("hello","session:你进入了HelloServlet资源");

        //req.getRequestDispatcher("/hello.jsp").forward(req,resp);

       // resp.getWriter().println("你进入了HelloServlet资源");
        //重定向
        resp.sendRedirect("/hello.jsp");

    }
}
